package com.led.datatype;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

import net.sf.json.JSONObject;

public class DataBasic {
	private static int IDX_ID = 1;
	private static int IDX_IDX_DATAS = 2;
	private static int IDX_USER_NAME = 3;
	private static int IDX_INFO = 4;
	
	int id;
	int idx_datas;
	String info;
	String userName;

	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getIdx_datas() {
		return idx_datas;
	}

	public void setIdx_datas(int idx_datas) {
		this.idx_datas = idx_datas;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String toJsonStr(){
		JSONObject js = new JSONObject();
		js.put("id", id);
		js.put("idx_datas", idx_datas);
		js.put("info", info);
		js.put("userName", userName);
		return js.toString();
	}

	public DataBasic() {
		
	}
	public DataBasic(ResultSet rs) throws SQLException {
		id = rs.getInt(IDX_ID);
		idx_datas = rs.getInt(IDX_IDX_DATAS);
		userName = rs.getString(IDX_USER_NAME);
		info = rs.getString(IDX_INFO);
	}
	public DataBasic(JSONObject js) {
		if(js.containsKey("id")){
		   id = js.getInt("id");
		}
		info = js.getString("info");
		idx_datas = js.getInt("idx_datas");
		userName = js.getString("userName");
	}
	
	@Override
	public String toString() {
		return toJsonStr();
	}
	
	public static LinkedList<DataBasic> getDatas(ResultSet rs){
		LinkedList<DataBasic> loadDatas = new LinkedList<DataBasic>();
		if(null == rs){
			return loadDatas;
		}
		
		try {
			while(rs.next()){
//				DataBasic data = new DataBasic();
//				data.id = rs.getInt(IDX_ID);
//				data.idx_datas = rs.getInt(IDX_IDX_DATAS);
//				data.userName = rs.getString(IDX_USER_NAME);
//				data.info = rs.getString(IDX_INFO);
//				loadDatas.add(data);
				loadDatas.add(new DataBasic(rs));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return loadDatas;
	}
	
}
